﻿using B.Z.BasicData.Domain.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace B.Z.BasicData.Infrastructure
{
    public class BaseRepository<T> : IBaseRepository<T> where T : BaseData
    {
        private readonly UserDbContext _db;

        public BaseRepository(UserDbContext db)
        {
            _db = db;
        }

        public async Task<int> Add(T entity)
        {
            await _db.Set<T>().AddAsync(entity);
            return await _db.SaveChangesAsync();
        }

        public  int Del(List<T> entity)
        {
            _db.Set<T>().RemoveRange(entity);
            return  _db.SaveChanges();
        }

        public IQueryable<T> GetAll()
        {
            return _db.Set<T>().AsQueryable();
        }

        public async Task<int> Update(T entity)
        {
             _db.Set<T>().Update(entity);
            return await _db.SaveChangesAsync();
        }

        public int UpdateRange(List<T> entity)
        {
            _db.Set<T>().UpdateRange(entity);
            return _db.SaveChanges();
        }
    }
}
